package com.fnfn.sdk.api.common;

import org.apache.commons.lang3.time.DateUtils;


import java.util.Calendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    public static String addDate(String date, Integer amount) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(DateUtils.addDays(simpleDateFormat.parse(date), amount));
    }

    public static String formatDateFile(Date date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
    
    /**
     * jlq
     * @param str
     * @return
     * @throws Exception
     */
    public static String formatDate(String str) throws Exception {
    	if(str!=null && !str.equals("")) {
    		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(str);
            return simpleDateFormat.format(date);
    	}
        return "";
    }

    public static String formatIotrDate(Date date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        return simpleDateFormat.format(date);
    }

    public static String formatDateTime(Date date) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String eMMTimeToDate(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US);
            Date date = sdf.parse(str);
            sdf = new SimpleDateFormat("yyyy/MM/dd");
            return sdf.format(date);
        } catch (Exception e) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            return sdf.format(new Date());
        }
    }

    /**
     * 指定日期加上天数后的日期
     * 
     * @param num 为增加的天数
     * @param newDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, String newDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Date currdate = format.parse(newDate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        return enddate;
    }

    /**
     * date2比date1多的天数
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(String dateStr1) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date date1 = format.parse(dateStr1);
            Date date2 = new Date();
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            int day1 = cal1.get(Calendar.DAY_OF_YEAR);
            int day2 = cal2.get(Calendar.DAY_OF_YEAR);

            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);
            if (year1 != year2) // 同一年
            {
                int timeDistance = 0;
                for (int i = year1; i < year2; i++) {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                    {
                        timeDistance += 366;
                    } else // 不是闰年
                    {
                        timeDistance += 365;
                    }
                }

                return timeDistance + (day1 - day2);
            } else // 不同年
            {
                return day1 - day2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
